package com.zhanghp.class009;

import com.zhanghp.common.DoubleListNode;
import com.zhanghp.common.ListNode;

/**
 * @author zhanghp
 * @since 2024/2/21 10:41
 */
public class ListReverse {

    // 反转单链表测试链接 : https://leetcode.cn/problems/reverse-linked-list/
    static class Solution{
        public static ListNode reverseList(ListNode head) {
            ListNode pre = null;
            ListNode cur = head;
            while (cur != null) {
                cur = head.next;
                head.next = pre;
                pre = head;
                head = cur;
            }
            return pre;
        }
    }

    // 双向链表没找到测试链接
    static class Solution2{
        public static DoubleListNode reverseDoubleList(DoubleListNode head) {
            // 上一个节点
            DoubleListNode pre = null;
            // 下一个节点
            DoubleListNode next = null;
            while (head != null) {
                // 下一个节点
                next = head.next;
                // 反转后的上一个节点
                head.last = next;
                // 反转后的下一个节点
                head.next = pre;
                pre = head;
                head = next;
            }
            return pre;
        }
    }
}
